package com.djs.lock.impl;

import com.djs.Director;
import com.djs.lock.ILock;
import com.djs.redis.impl.JedisOperator;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Description
 * @auther dengjingsi
 * @create 2020-03-16 17:16
 */
public class RedisLock implements ILock {
    /**
     * jedis操作类
     */
    private JedisOperator jedisOperator;
    /**
     * 需要加锁的key
     */
    private String key;
    /**
     * 默认锁过期时间 30s
     */
    private int lockTimeout = 30_000;

    public RedisLock() {
    }

    public RedisLock(String key) {
        this.key = key;
    }

    @Override
    public String lock() {
        return null;
    }

    @Override
    public String tryLock() {
        return null;
    }

    @Override
    public String tryLock(long tryLockTime, long timeout) {
        Jedis jedis = getJedis();
//        jedis.setex(getKey(),lockTimeout,)
        return "";
    }

    @Override
    public boolean checkValidLock(String lockId) {
        return false;
    }

    @Override
    public boolean unLock() {
        return false;
    }

    /**
     * 获取Jedis
     * @return
     */
    private Jedis getJedis(){
        if (null == jedisOperator){
//            jedisOperator = Director.getInstance().getJedisOperator();
        }
        return jedisOperator.getJedis();
    }

    public JedisOperator getJedisOperator() {
        return jedisOperator;
    }

    public void setJedisOperator(JedisOperator jedisOperator) {
        this.jedisOperator = jedisOperator;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}
